import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:my_meeting/entity/meeting_data.dart';
import 'package:my_meeting/extension/get_extension.dart';
import 'package:my_meeting/global.dart';
import 'package:my_meeting/utils/utils.dart';

import '../../../routes/router.dart';
import '../../../utils/log_utils.dart';

class Meeting_editLogic extends GetxController {
  var personArr = ['%+%'].obs;

  var meetingData = MeetingData().obs;

  var textEditController = TextEditingController();

  bool isEdit = false;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    debugPrint('add---${Get.arguments.toString()}');
    if (Get.arguments != null) {
      isEdit = true;
      meetingData = Get.arguments;
      textEditController.text = meetingData.value.meetingContent ?? '';
      getPersonArr();
    }
  }

  void getPersonArr(){
    personArr.clear();
    List<String> arr = meetingData.value.participants!.split(',');
    personArr.assignAll(arr);
    personArr.add('%+%');
    personArr.refresh();
  }

  void getTakeInPerson() {
    String takeInPerson = '';
    if (personArr.length > 1) {
      for (String person in personArr) {
        if (person != '%+%') {
          if (takeInPerson.isEmpty) {
            takeInPerson = person;
          } else {
            takeInPerson = '$takeInPerson,$person';
          }
        }
      }
      debugPrint(takeInPerson);
    }
    meetingData.value.participants = takeInPerson;
  }

  void confirmAction() {
    debugPrint(meetingData.value.toMap().toString());
    if (Utils.stringIsEmpty(meetingData.value.meetingContent)) {
      Get.showToast(content: '请输入会议内容',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.meetingBeginTime)) {
      Get.showToast(content: '请选择会议时间',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.meetingEndTime)) {
      Get.showToast(content: '请选择会议时间',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.meetingName)) {
      Get.showToast(content: '请选输入会议名称',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.meetingAddress)) {
      Get.showToast(content: '请输入会议地址',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.participants)) {
      Get.showToast(content: '请输入参会人',toastType: ToastType.warn);
      return;
    }
    if (Utils.stringIsEmpty(meetingData.value.deviceOpen)) {
      Get.showToast(content: '请选择设备开启时间',toastType: ToastType.warn);
      return;
    }
    if(isEdit){
      LogD("参与人${meetingData.value.participants}");
      global.updateMeeting(meetingData.value).then((value) {
        debugPrint('更新成功');
        Get.back(result: true);
      });
    }else{
      meetingData.value.createTime = DateTime.now().toString();
      global.insertMeeting(meetingData.value).then((value) {
        debugPrint('添加成功');
        Get.back(result: true);
      });
    }

  }

  void toAppointment() {
    Get.toNamed(AppRoutes.appointment)?.then((onValue) {
      if (onValue != null) {
        LogD('========toAppointment:  ${onValue}');
        String date = onValue['date'];
        String startTime = onValue['startTime'];
        String endTime = onValue['endTime'];
        int facilitieIndex = onValue['facilitieIndex'];
        meetingData.value.meetingBeginTime = '$date $startTime';
        meetingData.value.meetingEndTime = '$date $endTime';
        meetingData.value.facilities = facilitieIndex.toString();
        meetingData.refresh();
        LogD("beginTime--${meetingData.value.meetingBeginTime}");
        LogD("endTime--${meetingData.value.meetingEndTime}");
        LogD(
            '========toAppointment date: $date startTime: $startTime endTime: $endTime facilitieIndex: $facilitieIndex ');
      }
    });
  }
}
